package com.landun.market.web.domain;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.landun.market.pojo.Constants;
import com.landun.market.pojo.enums.Gender;
import com.landun.market.pojo.enums.StateCode;
import com.landun.market.pojo.enums.UserType;
import com.landun.market.pojo.model.User;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.ToString;

import java.util.Date;
import java.util.Optional;

/**
 * @author jiang.chuan
 * @date 2018-05-12
 */
@Data
@ToString
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
@JsonPropertyOrder({"id", "loginName", "name", "nickname", "email", "mobile", "gender", "genderDesc",
        "birthday", "company", "type", "typeDesc", "reserved", "expiryTime", "loginCount", "lastHost",
        "state", "stateDesc", "createdTime", "updatedTime", "children"})
public class UserVO extends BaseVO<UserVO> {
    private Integer id;
    private String loginName;
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private String password;
    private String name;
    private String nickname;
    private String email;
    private String mobile;
    private Gender gender;
    private String genderDesc;
    @JsonFormat(pattern = Constants.DATE_TIME_FORMAT, locale = "zh", timezone = "GMT+8")
    private Date birthday;
    private String company;
    private UserType type;
    private String typeDesc;
    private Boolean reserved;
    @JsonFormat(pattern = Constants.DATE_TIME_FORMAT, locale = "zh", timezone = "GMT+8")
    private Date expiryTime;
    private Integer loginCount;
    private String lastHost;
    private StateCode state;
    private String stateDesc;


    public UserVO(User user) {
        Optional.ofNullable(user).ifPresent(usr -> {
            this.id = usr.getId();
            this.loginName = usr.getLoginName();
            this.name = usr.getName();
            this.nickname = usr.getNickname();
            this.email = usr.getEmail();
            this.mobile = usr.getMobile();
            this.gender = usr.getGender();
            this.birthday = usr.getBirthday();
            this.company = usr.getCompany();
            this.type = usr.getType();
            this.reserved = usr.getReserved();
            this.expiryTime = usr.getExpiryTime();
            this.loginCount = usr.getLoginCount();
            this.lastHost = usr.getLastHost();
            this.state = usr.getState();
            this.setCreatedTime(usr.getCreatedTime());
            this.setUpdatedTime(usr.getUpdatedTime());

            Optional.ofNullable(this.gender).ifPresent(gender -> this.genderDesc = gender.desc());
            Optional.ofNullable(this.type).ifPresent(type -> this.typeDesc = type.desc());
            Optional.ofNullable(this.state).ifPresent(state -> this.stateDesc = state.desc());
        });
    }

    public User toEntity() {
        User user = new User();
        user.setId(this.id);
        user.setLoginName(this.loginName);
        user.setPassword(this.password);
        user.setName(this.name);
        user.setNickname(this.nickname);
        user.setEmail(this.email);
        user.setMobile(this.mobile);
        user.setGender(this.gender);
        user.setBirthday(this.birthday);
        user.setCompany(this.company);
        user.setType(this.type);
        user.setReserved(Optional.ofNullable(this.reserved).orElse(false));
        user.setExpiryTime(this.expiryTime);
        user.setState(Optional.ofNullable(this.state).orElse(StateCode.ACTIVE));

        return user;
    }
}
